package com.wlstock.funi.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.em.baselistener.OnBackListener;
import com.em.baselistener.OnMoreListener;
import com.em.widget.view.SelectPopUpWindow.onPopViewListener;
import com.wlstock.funi.R;
import com.wlstock.funi.adapter.MyAccountStockAdapter;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.dao.MyAccountStockDao;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.request.QuickSaile;
import com.wlstock.funi.runnable.SysHisTrade;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;
import com.wlstock.funi.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyAccountStockActivity extends FundBaseActivity implements OnMoreListener, onPopViewListener, OnBackListener, QuickSaile.QuickSellListener {
	
	private ListView lv_base;
	private List<MyAccountStocks> ownStocks;
	private MyAccountStockAdapter ownStockAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_base_lv);
		setTitle(getResources().getString(R.string.title_ownstock));
		showMoreBtn(this);
		onBackClick(this);
		
		lv_base = (ListView) findViewById(R.id.lv_base);
		ownStocks = MyAccountStockDao.getInstance(this).queryAllStocks();	
		ownStockAdapter = new MyAccountStockAdapter(this, ownStocks, R.layout.list_item_own_stock);
		
		lv_base.setAdapter(ownStockAdapter);
		lv_base.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
				MyAccountStocks myStock = ownStocks.get(arg2);
				QuickSaile saile = new QuickSaile(myStock.getStkCode(), myStock.getStkName(), String.valueOf(myStock.getStkQty()));
				saile.saileDialog(MyAccountStockActivity.this);
			}
		});

		lv_base.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final MyAccountStocks selectStock = ownStocks.get(position);
                new AlertDialog.Builder(MyAccountStockActivity.this).setTitle("手动删除").setMessage("确定删除"+selectStock.nameWithCode()+"?").setNegativeButton("取消",null).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
						ownStockAdapter.deleteData(selectStock);
                        MyAccountStockDao.getInstance(MyAccountStockActivity.this).deleteStock(selectStock.getStkCode());
                    }
                }).show();
                return true;
            }
		});


		if (FundApplication.isLogSuccess){
			SysHisTrade sysHisTrade = new SysHisTrade();
			sysHisTrade.request();
		}
	}

	@Override
	public void onMoreClick(View v) {
		// TODO Auto-generated method stub
		showPopUpView(getMenuList(), this);
	}

	private List<Map<String, Object>> getMenuList() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> menus = new ArrayList<Map<String,Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_clear_all));
		map.put("icon", R.drawable.actionbar_camera_icon);
		menus.add(map);
		
		return menus;
	}

	@Override
	public void onViewSelectListener(String str) {
		if (getResources().getString(R.string.pop_title_clear_all).equals(str)) {
			if (FundApplication.isLogSuccess) {
				List<MyAccountStocks> ownStocks = MyAccountStockDao.getInstance(this).queryAllStocks();
				for (int i = 0; i < ownStocks.size(); i++) {
					MyAccountStocks ownStock = ownStocks.get(i);
					QuickSaile quickSaile = new QuickSaile(ownStock.getStkCode(), ownStock.getStkName(), String.valueOf(ownStock.getStkQty()));
					quickSaile.saile(true);
					quickSaile.qsListener = this;
				}	
			}else {
				NotiUtils.showSimpleNoti(FundApplication.getInstance(), NotiIdGenerator.ID_LOG_OUTDATE, "登陆失效，请重新登陆", "无法执行一键清仓操作", R.drawable.ic_exception);
			}
			
		}
	}


	@Override
	public void onBackClick(View v) {
		this.finish();
	}

	@Override
	public void getSellInfo(boolean isSuccess, String stkCode, String stkName, float price, String pty) {
		String tip = "";
		if (isSuccess){
			tip = "成功";
		}else {
			tip = "失败";
		}
		String content = "闪电卖出"+stkName+"("+stkCode+")"+tip;
		ToastUtils.showMiddleMsg(this,content);
	}
}
